package io.github.talelin.latticy.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.latticy.dto.SpuDTO;
import io.github.talelin.latticy.mapper.SpuMapper;
import io.github.talelin.latticy.model.*;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author generator@TaleLin
 * @since 2021-10-25
 */
@Service
public class SpuService extends ServiceImpl<SpuMapper, SpuDO>{

    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SpuImgService spuImgService;

    @Autowired
    private SpuDetailImgService spuDetailImgService;

    @Autowired
    private SpuKeyService spuKeyService;

    public SpuDetailDO getDetail(Long id){
        return spuMapper.getDetail(id);
    }

    @Transactional
    public void create(SpuDTO dto){
        // 将spu基础信息写入到spuDO
        SpuDO spuDO = new SpuDO();
        BeanUtils.copyProperties(dto, spuDO);
        // 根据categoryId查询category，并返回rootCategoryId到spuDO
        CategoryDO categoryDO = categoryService.getById(spuDO.getCategoryId());
        spuDO.setRootCategoryId(categoryDO.getParentId());
        this.save(spuDO);

        // 以下是轮播图、详情图的逻辑
        List<String> spuImgList = new ArrayList<>();
        // 如果没有上传spuImgList，就用商品主图spu里的img来作为轮播图
        if( dto.getSpuImgList() == null){
            spuImgList.add(dto.getImg());
        }else {
            spuImgList = dto.getSpuImgList();
        }
        this.insertSpuImgList(spuImgList,spuDO.getId());

        if(dto.getDetailImgList() != null){
            this.insertSpuDetailImgList(dto.getDetailImgList(),spuDO.getId());
        }

        if(dto.getSpecKeyIdList() != null){
            this.insertSpuKeyList(dto.getSpecKeyIdList(),spuDO.getId());
        }
    }

    private void insertSpuDetailImgList(List<String> detailImgList, Long id) {
        List<SpuDetailImgDO> spuDetailImgDOList = new ArrayList<>();
        for (int i = 0 ; i < detailImgList.size(); i++){
            SpuDetailImgDO spuDetailImgDO = new SpuDetailImgDO();
            spuDetailImgDO.setImg(detailImgList.get(i))  // 赋值img
                    .setSpuId(id.intValue())                // 赋值外键
                    .setIndex(i);                        // 赋值index，用于排序
            spuDetailImgDOList.add(spuDetailImgDO);
        }
        this.spuDetailImgService.saveBatch(spuDetailImgDOList);
    }

    private void insertSpuImgList(List<String> spuImgList,Long spuId) {
        List<SpuImgDO> spuImgDOList = spuImgList.stream().map( s -> {
            SpuImgDO spuImgDO = new SpuImgDO();
            spuImgDO.setImg(s);
            spuImgDO.setSpuId(spuId.intValue());
            return spuImgDO;
        }).collect(Collectors.toList());
        this.spuImgService.saveBatch(spuImgDOList);
    }

    private void insertSpuKeyList(List<Long> spuKeyIdList, Long spuId){
        List<SpuKeyDO> spuKeyDOList = spuKeyIdList.stream()
                .map(sk -> {
                   SpuKeyDO spuKeyDO = new SpuKeyDO();
                   spuKeyDO.setSpuId(spuId.intValue());
                   spuKeyDO.setSpecKeyId(sk.intValue());
                   return spuKeyDO;
                }).collect(Collectors.toList());
        this.spuKeyService.saveBatch(spuKeyDOList);
    }

}
